/**
 *  test_pipe.c - Test codes for pipe and its functions.
 *                It runs on Windows or Linux.
 *
 *  Copyright (C) 2008  ZhangHu
 *  All rights reserved.
 *  E-MAIL: anmnmnly@gmail.com
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include "test_pipe.h"
#include "pipe_stubs.h"
#include "kernel/pipe.c"
#include "kernel/list.c"


static void test_pipeClear_0(void)
{
    /* Initializing argument 1 (pPipe) */
    pipe_t *_pPipe = 0;
    pipeClear(_pPipe);
}



static void test_pipeClear_1(void)
{
    /* Initializing argument 1 (pPipe) */
    struct pipe _pPipe_0 = {0};
    pipe_t *_pPipe = &_pPipe_0;

    /* Tested function call */
    pipeClear(_pPipe);
}



static void test_pipeDel_0(void)
{
    /* Initializing argument 1 (pPipe) */
    pipe_t *_pPipe = 0;

    /* Tested function call */
    pipeDel(_pPipe);
}



static void test_pipeDel_1(void)
{
    /* Initializing argument 1 (pPipe) */
    struct pipe _pPipe_0 = {0};
    pipe_t *_pPipe  = &_pPipe_0;

    /* Tested function call */
    pipeDel(_pPipe);
}



static void test_pipeInit_0(void)
{
    /* Initializing argument 1 (SendID) */
    word_t _SendID = 0;

    /* Initializing argument 2 (RevID) */
    word_t _RevID = 0;

    /* Initializing argument 3 (Size) */
    uword_t _Size = 0;

    /* Tested function call */
    pipe_t *_result = pipeInit(_SendID, _RevID, _Size);
    CU_ASSERT_PTR_NULL(_result);
}



static void test_pipeInit_1(void)
{
    /* Initializing argument 1 (SendID) */
    word_t _SendID = 1;

    /* Initializing argument 2 (RevID) */
    word_t _RevID = 0;

    /* Initializing argument 3 (Size) */
    uword_t _Size = 0;

    /* Tested function call */
    pipe_t *_result = pipeInit(_SendID, _RevID, _Size);
    CU_ASSERT_PTR_NULL(_result);
}



static void test_pipeInit_2(void)
{
    /* Initializing argument 1 (SendID) */
    word_t _SendID = ~0;

    /* Initializing argument 2 (RevID) */
    word_t _RevID = -1;

    /* Initializing argument 3 (Size) */
    uword_t _Size = 0;

    /* Tested function call */
    pipe_t *_result = pipeInit(_SendID, _RevID, _Size);
    CU_ASSERT_PTR_NULL(_result);
}



static void test_pipeInit_3(void)
{
    /* Initializing argument 1 (SendID) */
    word_t _SendID = -1;

    /* Initializing argument 2 (RevID) */
    word_t _RevID = 4;

    /* Initializing argument 3 (Size) */
    uword_t _Size = 1;

    /* Tested function call */
    pipe_t *_result = pipeInit(_SendID, _RevID, _Size);
    CU_ASSERT_PTR_NOT_NULL(_result);
}



static void test_pipeInit_4(void)
{
    /* Initializing argument 1 (SendID) */
    word_t _SendID = 0;

    /* Initializing argument 2 (RevID) */
    word_t _RevID = 0;

    /* Initializing argument 3 (Size) */
    uword_t _Size = 1;

    /* Tested function call */
    pipe_t *_result = pipeInit(_SendID, _RevID, _Size);
    CU_ASSERT_PTR_NULL(_result);
}



static void test_pipeInit_5(void)
{
    /* Initializing argument 1 (SendID) */
    word_t _SendID = 1;

    /* Initializing argument 2 (RevID) */
    word_t _RevID = -1;

    /* Initializing argument 3 (Size) */
    uword_t _Size = 4;

    /* Tested function call */
    pipe_t *_result = pipeInit(_SendID, _RevID, _Size);
    CU_ASSERT_PTR_NOT_NULL(_result);
}



static void test_pipeInit_6(void)
{
    /* Initializing argument 1 (SendID) */
    word_t _SendID = 0;

    /* Initializing argument 2 (RevID) */
    word_t _RevID = 4;

    /* Initializing argument 3 (Size) */
    uword_t _Size = ~0;

    /* Tested function call */
    pipe_t *_result = pipeInit(_SendID, _RevID, _Size);
    CU_ASSERT_PTR_NULL(_result);
}



static void test_pipeInit_7(void)
{
    /* Initializing argument 1 (SendID) */
    word_t _SendID  = 0;

    /* Initializing argument 2 (RevID) */
    word_t _RevID  = 0;

    /* Initializing argument 3 (Size) */
    uword_t _Size = ~0;

    /* Tested function call */
    pipe_t *_result = pipeInit(_SendID, _RevID, _Size);
    CU_ASSERT_PTR_NULL(_result);
}



static void test_pipeInit_8(void)
{
    /* Initializing argument 1 (SendID) */
    word_t _SendID = 4;

    /* Initializing argument 2 (RevID) */
    word_t _RevID = -1;

    /* Initializing argument 3 (Size) */
    uword_t _Size = 1u;

    /* Tested function call */
    pipe_t *_result = pipeInit(_SendID, _RevID, _Size);
    CU_ASSERT_PTR_NOT_NULL(_result);
}



static void test_pipeInit_9(void)
{
    /* Initializing argument 1 (SendID) */
    word_t _SendID = -1;

    /* Initializing argument 2 (RevID) */
    word_t _RevID = 4;

    /* Initializing argument 3 (Size) */
    uword_t _Size = 0u;

    /* Tested function call */
    pipe_t *_result = pipeInit(_SendID, _RevID, _Size);
    CU_ASSERT_PTR_NULL(_result);
}



static void test_pipeRev_0(void)
{
    /* Initializing argument 1 (pPipe) */
    pipe_t *_pPipe = 0;

    /* Tested function call */
    void *_result = pipeRev(_pPipe);
    CU_ASSERT_PTR_NULL(_result);
}



static void test_pipeRev_1(void)
{
    /* Initializing argument 1 (pPipe) */
    struct pipe _pPipe_0 = {0};
    pipe_t *_pPipe = &_pPipe_0;

    /* Tested function call */
    void *_result = pipeRev(_pPipe);
    CU_ASSERT_PTR_NULL(_result);
}



static void test_pipeSend_0(void)
{
    /* Initializing argument 1 (pPipe) */
    pipe_t *_pPipe = 0;

    /* Initializing argument 2 (pMsg) */
    char *_pMsg_0 = "";
    void *_pMsg = (void*)_pMsg_0;

    /* Tested function call */
    uword_t _result  = pipeSend(_pPipe, _pMsg);
    CU_ASSERT_EQUAL(_result, 0);
}



static void test_pipeSend_1(void)
{
    /* Initializing argument 1 (pPipe) */
    struct pipe _pPipe_0 = {0};
    pipe_t *_pPipe = &_pPipe_0;

    /* Initializing argument 2 (pMsg) */
    char *_pMsg_1 = "";
    void *_pMsg = (void*)_pMsg_1;

    /* Tested function call */
    uword_t _result  = pipeSend(_pPipe, _pMsg);
    CU_ASSERT_EQUAL(_result, 0);
}




static void test_pipeSend_2(void)
{
    /* Initializing argument 1 (pPipe) */
    pipe_t *_pPipe = 0;

    /* Initializing argument 2 (pMsg) */
    char *_pMsg_0 = 0;
    void *_pMsg = (void*)_pMsg_0;

    /* Tested function call */
    uword_t _result  = pipeSend(_pPipe, _pMsg);
    CU_ASSERT_EQUAL(_result, 0);
}



static void test_pipeSend_3(void)
{
    /* Initializing argument 1 (pPipe) */
    struct pipe _pPipe_0 = {0};
    pipe_t *_pPipe = &_pPipe_0;

    /* Initializing argument 2 (pMsg) */
    char *_pMsg_1 = 0;
    void *_pMsg = (void*)_pMsg_1;

    /* Tested function call */
    uword_t _result = pipeSend(_pPipe, _pMsg);
    CU_ASSERT_EQUAL(_result, 0);
}



#define PIPE_SIZE 8
#define SEND_ID 100
#define REV_ID 101

static void test_pipe_uniting(void) {
    word_t i;
    char_t *str = NULL;
    pipe_t *pipe = ((void*)0);
    char msg[][10] = {"Message 0", "Message 1", "Message 2", "Message 3",
                      "Message 4","Message 5", "Message 6", "Message 7"};

    pipe = pipeInit(SEND_ID, REV_ID, PIPE_SIZE);
    CU_ASSERT_PTR_NOT_NULL(pipe);

    set_id(SEND_ID);
    CU_ASSERT_EQUAL(current()->id, SEND_ID);

    for(i=0; i<PIPE_SIZE; i++) {
        pipeSend(pipe, msg[i]);
    }
    CU_ASSERT_EQUAL(pipeGetUsed(pipe), PIPE_SIZE);

    set_id(REV_ID);
    CU_ASSERT_EQUAL(current()->id, REV_ID);
    for(i=0; i<PIPE_SIZE; i++) {
        str = (char*)pipeRev(pipe);
        CU_ASSERT_PTR_EQUAL(str, msg[i]);
    }
    CU_ASSERT_EQUAL(pipeGetUsed(pipe), 0);

    set_id(SEND_ID);
    for(i=0; i<PIPE_SIZE; i++) {
        pipeSend(pipe, msg[i]);
    }

    CU_ASSERT_EQUAL(pipeGetUsed(pipe), PIPE_SIZE);
    pipeClear(pipe);
    CU_ASSERT_EQUAL(pipeGetUsed(pipe), 0);

    pipeDel(pipe);
}



int test_pipe_init(void) {
    printf("pipe test ...\n\n");
    return 0;
}



int test_pipe_clean(void) {
    printf("pipe test is over!\n\n");
    return 0;
}



CU_TestInfo test_pipe_cases[] = {
    {"test_pipeClear_0", test_pipeClear_0},
    {"test_pipeClear_1", test_pipeClear_1},
    {"test_pipeDel_0", test_pipeDel_0},
    {"test_pipeDel_1", test_pipeDel_1},
    {"test_pipeInit_0", test_pipeInit_0},
    {"test_pipeInit_1", test_pipeInit_1},
    {"test_pipeInit_2", test_pipeInit_2},
    {"test_pipeInit_3", test_pipeInit_3},
    {"test_pipeInit_4", test_pipeInit_4},
    {"test_pipeInit_5", test_pipeInit_5},
    {"test_pipeInit_6", test_pipeInit_6},
    {"test_pipeInit_7", test_pipeInit_7},
    {"test_pipeInit_8", test_pipeInit_8},
    {"test_pipeInit_9", test_pipeInit_9},
    {"test_pipeRev_0", test_pipeRev_0},
    {"test_pipeRev_1", test_pipeRev_1},
    {"test_pipeSend_0", test_pipeSend_0},
    {"test_pipeSend_1", test_pipeSend_1},
    {"test_pipeSend_2", test_pipeSend_2},
    {"test_pipeSend_3", test_pipeSend_3},
    {"test_pipe_uniting", test_pipe_uniting},
    CU_TEST_INFO_NULL
};
